package com.ubxtech.web.aspect;

import com.alibaba.fastjson2.JSONObject;
import com.ubxtech.core.context.ContextUtil;
import com.ubxtech.web.properties.AuthProperties;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * @author Victor.Xiao
 * @since 2022-01-14 17:53
 **/
@Aspect
@Component
@Slf4j
@Order(1)
public class RespAspect {

    @Resource
    private AuthProperties authProperties;

    @Pointcut("execution(* com.ubxtech..controller..*.*(..))")
    private void controllerResp(){
    }

    @Around("controllerResp()")
    public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
        Object result = null;
        //拿到入参
        Object[] params = pjp.getArgs();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String url = request.getRequestURL().toString();

        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        Long curMills = System.currentTimeMillis();
        List<Object> noneFileParams = new ArrayList<>();

        //把body设置到requestAttribute中
        if(params!=null&&params.length>0){
            for (Object param : params) {
                //还有空的情况
                if (param == null) {
                    continue;
                }
                if (param instanceof MultipartFile) {
                    continue;
                }
                if (param.getClass().isArray() && param.getClass().getName().contains("MultipartFile")) {
                    continue;
                }
                noneFileParams.add(param);
            }
        }
        boolean printLog = true;
        if (authProperties.getPrintLogExcludePath() != null && authProperties.getPrintLogExcludePath().size() > 0) {
            for (String pathExclude : authProperties.getPrintLogExcludePath()) {
                if (url.contains(pathExclude)) {
                    printLog = false;
                    break;
                }
            }
        }
        try {
            String noneFileParamsStr = JSONObject.toJSONString(noneFileParams);
            ContextUtil.set("noneFileParamsStr", noneFileParamsStr);
            if (printLog || log.isDebugEnabled()) {
                log.info("api请求地址:[{}];uid:{},time:{},入参:[{}]", url, uuid, curMills, noneFileParamsStr);
            }
        } catch (Exception e) {
            log.warn("api请求地址:[{}];uid:{},time:{},入参不是json;",url,uuid,curMills);
        }
        //结果
        result = pjp.proceed();
        if (printLog || log.isDebugEnabled()) {
            log.info("api请求地址:[{}];出参:[{}];uid:{},time:{}",url,result,uuid,(System.currentTimeMillis()-curMills));
        }
        return result;
    }


}
